import Common from "../Common";
import Tr from "../common/table/Tr";
import {isArray} from "../common/Helpers";

const {ccclass, property} = cc._decorator;

@ccclass
export default class DrawPage extends Common {

    @property(cc.Node)
    backBtn: cc.Node;

    @property(cc.Node)
    descBtn: cc.Node;

    @property(cc.Node)
    drawBtn: cc.Node;

    @property(cc.Node)
    descToast: cc.Node;

    @property(cc.Node)
    drawConfirmToast: cc.Node;

    @property(cc.Node)
    drawConfirmBtn: cc.Node;

    @property(cc.Node)
    drawCancelBtn: cc.Node;

    @property(cc.Label)
    descLabel: cc.Label;

    @property(cc.Label)
    amountLabel: cc.Label

    @property(cc.Label)
    batchLabel: cc.Label

    @property(cc.Label)
    currentLabel: cc.Label

    @property(cc.Label)
    expectLabel: cc.Label


    @property(cc.Label)
    firstLabel: cc.Label

    @property(cc.Label)
    secondLabel: cc.Label

    @property(cc.Label)
    thirdLabel: cc.Label

    @property(cc.Node)
    listContent: cc.Node


    trItem: cc.Prefab

    protected watch = {
        userInfo: (val) => {
            const account = val.account || {}
            this.amountLabel.string = account.cash_account || '0.00'
            this.descLabel.string = val.draw_desc || ''
        }
    }

    start() {
        cc.director.preloadScene('hall')
        this.initEmitter()
        this.initSetting()
        this.initLists()
    }

    /**
     * 事件监听
     */
    protected initEmitter() {
        this.backBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            cc.director.loadScene('hall')
        }, this)

        // 右上角抽奖说明按钮
        this.descBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            this.descToast.active = true
        }, this)

        // 参加抽奖按钮
        this.drawBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick();
            this.drawConfirmToast.active = true
        }, this)

        // 取消抽奖按钮
        this.drawCancelBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick();
            this.drawConfirmToast.active = false
        }, this)

        // 确定抽奖按钮
        this.drawConfirmBtn.on(cc.Node.EventType.TOUCH_START, this.onDraw, this)
    }


    /**
     * 点击抽奖按钮
     * @protected
     */
    protected async onDraw() {
        try {
            this.onBtnClick()
            const {message} = await this.http.post('bonus/draw', {})
            this.showToast(message)
            this.initLists();
            const userInfo = await this.initUserInfo(true)
            if (userInfo) {
                this._data.userInfo = userInfo
            }
            this.drawConfirmToast.active = false
        } catch ({message}) {
            this.showToast(message)
        }
    }

    protected async initLists() {
        try {
            const {data} = await this.http.get('bonus/currentBatchRecords', {})
            const {batch, amounts, lists} = data || {}
            this.batchLabel.string = batch?.sn || '--' // 当前期号
            this.currentLabel.string = amounts || '--' // 当前奖金池
            this.clearTr();
            await this.attachTr(['ID', '抽奖时间', '抽奖金额'])
            if (isArray(lists)) {
                lists.forEach(item => {
                    this.attachTr([item.id, item.created_at, item.amounts])
                })
            }
        } catch ({message}) {
            this.showToast(message)
        }
    }

    protected async initSetting() {
        try {
            const {data} = await this.http.get('bonus/drawSetting', {})
            const {first_prize, second_prize, third_prize, price_pool} = data || {}
            this.expectLabel.string = price_pool || '--' // 开奖奖金池阈值
            this.firstLabel.string = first_prize || '--' // 一等奖奖金
            this.secondLabel.string = second_prize || '--' // 二等奖奖金
            this.thirdLabel.string = third_prize || '--' // 三等奖奖金
        } catch ({message}) {
            this.showToast(message)
        }
    }

    protected async attachTr(data: any[], bgColor?: string) {
        const trNode = cc.instantiate(await this.getTrItem())
        const tr = trNode.getComponent(Tr)
        data.forEach(item => {
            if (item && typeof item === 'object') {
                tr.attachTd(`${item.label}`, item.width || 200)
            } else {
                tr.attachTd(`${item}`)
            }

        })
        tr.setBgColor(bgColor) // 不生效？
        this.listContent.addChild(trNode)
    }

    protected async getTrItem() {
        if (!this.trItem) {
            this.trItem = await this.getPrefab('table/item')
        }
        return this.trItem
    }

    protected clearTr() {
        this.listContent.removeAllChildren(true)
    }

}
